package com.crp.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSONArray;

/**
 * @author jincongmin
 */
public class DigestUtil {
	public static String digest(String key, String content) {
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            byte[] secretByte = key.getBytes("utf-8");
            byte[] dataBytes = content.getBytes("utf-8");

            SecretKey secret = new SecretKeySpec(secretByte, "HmacSHA1");
            mac.init(secret);

            byte[] doFinal = mac.doFinal(dataBytes);
            byte[] encodeBase64 =Base64.encodeBase64(doFinal);
            return new String(encodeBase64, "utf-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String digest(String key, Map<String, ?> map) {
        StringBuilder s = new StringBuilder();
        List<String> list = new ArrayList<String>();
        
//        Long timestamp=(Long)map.get(Constants.TIMESTAMP);
//        Long curTime=System.currentTimeMillis();
        
        for(Map.Entry<String, ?> entry:map.entrySet()){
            list.add(entry.getKey());
        }
        Collections.sort(list);
        for (String str : list) {
            Object values=map.get(str);
            if(values==null){
                continue;
            }else{
                if(values.getClass().isArray()){
                    continue;
                }else if(values instanceof List){
                    continue;
                }else if(values instanceof Set){
                    continue;
                }else if(values instanceof Map){
                    continue;
                }else if(values instanceof Collection){
                    continue;
                }else if(values instanceof String[]){
                    continue;
                }else if(values instanceof JSONArray){
                    continue;
                }else{
                    s.append(values.toString());
                }
            }
        }
        return digest(key, s.toString());
    }
    
    
    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr1="2016-07-11 16:06:00";
        String dateStr2="2016-07-11 15:13:00";
        
        Date date1=sdf.parse(dateStr1);
        Date date2=sdf.parse(dateStr2);
        
        long time=System.currentTimeMillis()-date1.getTime();
        System.out.println(time/1000/60);
	}
}
